package com.cysnake.quitsmoke.activity;

import java.util.Calendar;

import com.cysnake.quitsmoke.R;
import com.cysnake.quitsmoke.VO.PlanVO;
import com.cysnake.quitsmoke.common.dao.PlanDao;
import com.cysnake.quitsmoke.tools.FormatTools;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

public class PlanActivity extends Activity {
	/** Called when the activity is first created. */
	EditText dayCigaretteText;
	Button beginDateButton;
	Button endDateButton;
	Button submitButton;
	Calendar beginDate = Calendar.getInstance();
	Calendar endDate = Calendar.getInstance();
	PlanDao planDao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.plan);
		modify();
	}

	private void modifyButton() {
		submitButton = (Button) findViewById(R.id.plan_Submit_Button);
		submitButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("test",
						"beginDate.before(endDate)      "
								+ beginDate.before(endDate));
				Log.d("test", "dayCigaretteText.getText() != null     "
						+ (dayCigaretteText.getText() != null));
				if (beginDate.before(endDate)
						&& dayCigaretteText.getText() != null) {
					Log.d("test", "get in!");
					PlanVO plan = new PlanVO();
					plan.setSmokeNum(Integer.parseInt(dayCigaretteText
							.getText().toString()));
					plan.setStartTime(beginDate.getTime());
					plan.setEndTime(endDate.getTime());
					planDao = planDao.open();
					if (planDao.getPlanById(1) != null) {
						planDao.update(plan);
					} else {
						planDao.create(plan);
					}
					planDao.close();
					new AlertDialog.Builder(PlanActivity.this)
							.setTitle(getString(R.string.plan_dailog_title))
							.setMessage(
									getString(R.string.plan_dailog_message_success))
							.setPositiveButton(
									getString(R.string.plan_button_submit),
									null).show();
				} else {
					new AlertDialog.Builder(PlanActivity.this)
							.setTitle(getString(R.string.plan_dailog_title))
							.setMessage(getString(R.string.plan_dailog_message))
							.setPositiveButton(
									getString(R.string.plan_button_submit),
									null).show();
				}
			}
		});

		beginDateButton = (Button) findViewById(R.id.plan_beginDate_button);
		beginDateButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new DatePickerDialog(PlanActivity.this,
						new OnDateSetListener() {

							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								beginDate.set(year, monthOfYear, dayOfMonth);
								beginDateButton.setText(FormatTools
										.calenderToString(beginDate));
								Log.d("test",
										"beginDate:     "
												+ FormatTools
														.calenderToString(beginDate));
							}
						}, beginDate.get(Calendar.YEAR), beginDate
								.get(Calendar.MONTH), beginDate
								.get(Calendar.DAY_OF_MONTH)).show();

			}
		});

		endDateButton = (Button) findViewById(R.id.plan_endDate_button);
		endDateButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new DatePickerDialog(PlanActivity.this,
						new OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								endDate.set(year, monthOfYear, dayOfMonth);
								endDateButton.setText(FormatTools
										.calenderToString(endDate));
								Log.d("test",
										"endDate:     "
												+ FormatTools
														.calenderToString(endDate));
							}
						}, endDate.get(Calendar.YEAR), endDate
								.get(Calendar.MONTH), endDate
								.get(Calendar.DAY_OF_MONTH)).show();

			}
		});

	}

	private void modifyEditText() {
		dayCigaretteText = (EditText) findViewById(R.id.dayCigaretteText);
	}

	private void modify() {
		modifyButton();
		modifyEditText();
		planDao = new PlanDao(PlanActivity.this);
	}
}